// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
 * System_StdoutWriter.cpp
 *
 *  Created on: Jan 8, 2010
 *      Author: dhovemey
 */

#include <cassert>
#include <cstdint>
#include <cstdio>
#include <string>
#include "interp/VM_Interpreter.h"
#include "interp/VM_Object.h"
#include "interp/VM_MagicMethod.h"

extern "C"
VM_Object* System_StdoutWriter_printString(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	if (numArgs != 2 || !VM_INTERP->isType(VM_METHOD_ARG(1), CLASS_ID_STRING)) {
		// TODO: throw exception?
		return VM_INTERP->getNil();
	}

	std::wstring *str = (std::wstring *) VM_METHOD_ARG(1)->getMagicPtr();
	printf("%ls", str->c_str());
	//if (str->size() > 0 || (*str)[str->size() - 1] == L'\n') {
		fflush(stdout);
	//}

	return VM_RECEIVER;
}
